const COS = require('../../../utils/cos-wx-sdk-v5.js');
const cos = new COS({
	SecretId: 'AKIDYyUjwt34oH1U9YxZ9i13oIU93gDTY9ll',
	SecretKey: 'qQi012oQYOzILccMpKZxYuXtNbYFfpjN'
});
const app = getApp();
import { getCurrentPageUrlWithArgs } from '../../../utils/index';
const $api = require('../../../service/api.js').API;
Page({
	/**
   * 页面的初始数据
   */
	data: {
		fileList: [
			{
			  url: 'https://img.yzcdn.cn/vant/leaf.jpg',
			},
			{
			  url: 'https://img.yzcdn.cn/vant/tree.jpg',
			  deletable: false,
			},
		  ],
		imgUrl: app.globalData.imgUrl,
		fileList: [
			{
				deletable: false,
			},
		],
		price: 0,
		radio: '1',
		array: [],
		index: 0,
		username: '',
		address: '',
		wechat: '',
		phone: '',
		type: 0,
		wt_id: 1,
		content: ''
	},

	uploadimg() {
		var _this = this;
		wx.chooseImage({
			count: 1, // 默认9
			sizeType: [ 'original' ], // 可以指定是原图还是压缩图，默认用原图
			sourceType: [ 'album', 'camera' ], // 可以指定来源是相册还是相机，默认二者都有
			success: function(res) {
				let filePath = res.tempFiles[0].path;
				let filename = filePath.substr(filePath.lastIndexOf('/') + 1);
				_this.setData({
					['fileList.[' + 0 + '].url']: filePath
				});

				cos.postObject(
					{
						Bucket: 'repair-1259727252',
						Region: 'ap-guangzhou',
						Key: filename,
						FilePath: filePath,
						onProgress: function(info) {
							console.log(JSON.stringify(info));
						}
					},
					function(err, data) {
						console.log(err || data.Location);
						wx.setStorageSync('imgUrl', 'https://' + data.Location);
						wx.showToast({
						  title: '上传成功',
						})
					}
				);
			}
		});
	},

	onChange(event) {
		// event.detail 为当前输入的值
		console.log(event.detail);
		this.setData({
			radio: event.detail
		});
	},

	getText(e) {
		let str=e.detail.value;
		if(str.length<4){
			wx.showToast({
			  title: '详情不能少于4个字符',
			  icon:'none',
			  duration:2000
			})
			return false;
		}
		this.setData({
			content: e.detail.value
		});
	},

	bindPickerChange(e) {
		console.log(e);
		const index = e.detail.value;
		this.setData({
			index: index,
			price: this.data.array[index].price,
			wt_id: index
		});
		console.log(index);
		
	},

	first() {
		const phone = wx.getStorageSync('phoneNum');
		this.setData({
			phone: phone,
			wechat: phone
		});
	},

	onChangeInput(e) {
		let str=e.detail.value;
		let name=e.currentTarget.dataset.prop;
		if(name=='username'){
			if(str.length<2){
				wx.showToast({
				icon:'none',
				title: '报修人不能少于2位字符',
				duration:2000
				})
				return false;
			}
		}else
		if(name=='address'){
			if(str.length<4){
				wx.showToast({
				icon:'none',
				title: '宿舍地址不能少于4位字符',
				duration:2000
				})
				return false;
			}
		}
		else
		if(name=='wechat'){
			if(str.length<6){
				wx.showToast({
				icon:'none',
				title: '微信号不能少于6位字符',
				duration:2000
				})
				return false;
			}
		}
		else
		if(name=='phone'){
			if(str.length<11){
				wx.showToast({
				icon:'none',
				title: '手机号不能少于11位字符',
				duration:2000
				})
				return false;
			}
		}
		
		
		console.log(e.detail.value); //input 的 value
		console.log(e.currentTarget.dataset.prop); //data-prop 绑定的字符串，以此来确定改变的是哪个变量
		this.setData({
			[e.currentTarget.dataset.prop]: e.detail.value
		});
	},

	onSubmit() {
		let that=this;
		if(that.data.content.length>=4&&that.data.phone.length==11&&that.data.wechat.length>=6&&that.data.username.length>=2){
		const data = {
			type: this.data.type,
			wt: this.data.wt_id,
			style: this.data.radio,
			name: this.data.username,
			address: this.data.address,
			wx: this.data.wechat,
			phone: this.data.phone,
			imageUrl: wx.getStorageSync('imgUrl'),
			status: 0,
			content: this.data.content,
			openid: wx.getStorageSync('openid')
		}
		console.log(data)
		$api.submitOrder(data).then((res) => {
			console.log(res);
			if(res.code==11){
			wx.showToast({
				title: res.message,
				icon: 'success',
				duration: 2000
			});
			wx.requestSubscribeMessage({
				  tmplIds: ['RAzc1IgVnm6CZmKhiZIcHatVjTq0aJxdTO3MPuFO08Y'],
				});
			wx.navigateBack({
				delta: 1,
			  });
		}else {
			wx.showToast({
				title:res.message,
				icon:'none',
				duration:2000
			})
		}
		})
	}else{
		wx.showToast({
		  title: '请检查各信息字符数',
		  icon:'none'
		})
	}
	},

	/**
   * 生命周期函数--监听页面加载
   */
	onLoad: function() {},

	/**
   * 生命周期函数--监听页面初次渲染完成
   */
	onReady: function() {},

	/**
   * 生命周期函数--监听页面显示
   */
	onShow: function() {
		const token = wx.getStorageSync('token');
		if (!token) {
			wx.navigateTo({
				url: '/pages/User/Login/login'
			});
		} else {
			// 获取首页传过来的问题分类id
			const aa = getCurrentPageUrlWithArgs();
			console.log(aa.id);

			const data = {
				openid: wx.getStorageSync('openid').toString(),
				token: wx.getStorageSync('token').toString()
			};
			$api
				.getRepairType(aa.id, data)
				.then((res) => {
					if (res.flag === true) {
						console.log(res.data);
						this.setData({
							array: res.data,
							price: res.data[0].price,
							type: aa.id
						});
						console.log(this.data.price);
					}
				})
				.catch((err) => {
					console.log(err);
					wx.clearStorage();
					wx.navigateTo({
						url: '/pages/User/Login/login'
					});
				});
		}
		this.first();
	},

	/**
   * 生命周期函数--监听页面隐藏
   */
	onHide: function() {},

	/**
   * 生命周期函数--监听页面卸载
   */
	onUnload: function() {},

	/**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function() {},

	/**
   * 页面上拉触底事件的处理函数
   */
	onReachBottom: function() {},

	/**
   * 用户点击右上角分享
   */
	onShareAppMessage: function() {}
});
